package com.ccom.redis.client;

import com.alibaba.fastjson.JSONObject;
import com.ccom.core.common.model.BaseMap;
import com.ccom.redis.constant.GlobalConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

/**
 * redis 消息客户端
 */
@Slf4j
@Configuration
public class CcomRedisClient {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 发送消息
     *
     * @param handlerName 消息对应的监听处理类的类名，在RedisReceiver中通过spring容器获取对应的处理器对象，完成方法的执行
     * @param params
     */
    public void sendMessage(String handlerName, BaseMap params) {
        log.debug("sendMessage handlerName:{},params:{}", handlerName, JSONObject.toJSONString(params));
        params.put(GlobalConstants.HANDLER_NAME, handlerName);
        redisTemplate.convertAndSend(GlobalConstants.REDIS_TOPIC_NAME, params);
    }
}
